package com.hyh.toyshopping.controller;

import com.hyh.toyshopping.commons.CurUser;
import com.hyh.toyshopping.commons.Result;
import com.hyh.toyshopping.model.Orders;
import com.hyh.toyshopping.model.view.OrderDescView;
import com.hyh.toyshopping.model.view.OrderView;
import com.hyh.toyshopping.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @version 1.0
 * @BelongsProject:IntelliJ IDEA
 * @BelongsPackage:com.hyh.toyshopping.controller
 * @Author:hyhWTX
 * @ClassName:OrderController
 * @CreateTime:2022年-12月-15日 12:54
 * @Description: TODO (一句话描述以下该类的功能)
 */
@RestController
@RequestMapping("/orders")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody Orders orders, HttpSession session){
        //从session获得当前登录者编号user_id
        CurUser curUser = (CurUser) session.getAttribute("curUser");
        //将用户编号设置到orders对象中
        orders.setUser_id(curUser.getUser_id());
        //调用service层添加订单方法，将orders对象传入
        orderService.addOrder(orders);
        return Result.success("订单已生成");
    }
    /***
     * @Description: 根据用户编号获得订单信息
     * @param user_id
     * @return List<Orders>

     */

    @GetMapping("/getOrderByUser")
    public List<Orders> getOrderByUser(int user_id){
        return orderService.getOrderByUser(user_id);
    }


    @GetMapping("/getOrderDescByOrderId")
    public List<OrderDescView> getOrderDescByOrderId(String order_id){
        return orderService.getOrderDescByOrderId(order_id);
    }

    /***
     * @Description: 获得所有订单信息
     * @return List<OrderView>

     */

    @GetMapping("/getOrderList")
    public List<OrderView> getOrderList(){
        return orderService.getOrderList();
    }


    @PutMapping("/updateOrderStatus")
    public Result updateOrderStatus(String order_id,int order_status){
        orderService.updateOrderStatus(order_id,order_status);
        if(order_status==1){
            return Result.success("已发货");
        }else if(order_status==2){
            return Result.success("已确认收货");
        }else{
            return Result.success("已申请退款");
        }

    }
}
